package ts.group.webapp.market.controller.web;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ts.group.webapp.market.common.core.util.PubFun;
import ts.group.webapp.market.common.core.util.ResponseData;
import ts.group.webapp.market.common.core.web.controller.BaseController;
import ts.group.webapp.market.model.TDistributor;
import ts.group.webapp.market.model.TProductOrder;
import ts.group.webapp.market.service.IDistService;
import ts.group.webapp.market.service.IOrderService;

import com.starit.common.dao.support.Pagination;

/**
 * !	  
	* @Title: 订单管理
	* @Description: 1,系统生成订单编号(根据日期生成) 2,订单编辑时不能修改(由系统更新实现)
	* @author 		naluv125@hotmail.com
	* @date 			2013-8-4 上午4:02:38   
	* @version    	V1.0
	*
 */
@Controller
@RequestMapping("/ord")
public class OrderController extends BaseController{
	
	@Autowired
	private IOrderService  IOrderService;

	@Autowired
	private IDistService	IDistService; 
	
	
	/**
	 * 新增订单，只接受POST请求
	 */
	@RequestMapping(value = "/insertTord", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData insertTOrder(TProductOrder order) throws Exception {
		TDistributor dist =   IDistService.getDistributorByCode(order.getDistributorCode()).get(0);
		order.setDistributorId(dist.getId());
		order.setOrderCode(String.valueOf(PubFun.genOrderCode()));	//订单编号
		order.setFloors(dist.getFloors());
		order.setSumPrice(order.getSumPrice());
		order.setCreateTime(new Date());
		order.setSaleTime(new Date());
		IOrderService.createOrder(order);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	
	/**
	 * 更新订单, 只接受POST请求
	 * 订单编号不能更新
	 */
	@RequestMapping(value = "/updateTord", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTOrder(TProductOrder order) {
		TProductOrder old = IOrderService.getOrderById(order.getId());
		TDistributor dist =   IDistService.getDistributorByCode(order.getDistributorCode()).get(0);
		old.setDistributorCode(order.getDistributorCode());
		old.setProductCode(order.getProductCode());
		old.setProductPrice(order.getProductPrice());
		old.setDistributorId(dist.getId());
		old.setShopCode(order.getShopCode());
		old.setFloors(dist.getFloors());
		old.setSaleNumber(order.getSaleNumber());
		old.setSumPrice(order.getProductPrice()*order.getSaleNumber());
		old.setBook(order.getBook());
		old.setShopName(order.getShopName());
		old.setDistributorName(order.getDistributorName());
		old.setProductName(order.getProductName());
		old.setRemark(order.getRemark());
		IOrderService.updateOrder(old);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	/**
	 * 删除订单, 只接受POST请求
	 */
	@RequestMapping(value = "/removeTord", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData removeTOrder(Long[] ids) {
		for(Long orderId : ids){
			IOrderService.removeOrder(orderId);
		}
		return ResponseData.SUCCESS_NO_DATA;
	}

	
	/**
	 * 订单管理，查询订单信息，按照订单优先级降序排序
	 */
	@RequestMapping("/pageQueryTord")
	@ResponseBody
	public Pagination<Object> pageQueryTOrders(
			@RequestParam("start") int startIndex,
			@RequestParam("limit") int pageSize, TProductOrder order,
			@RequestParam(required = false)String startTime, 
			@RequestParam(required = false)String  endTime,
			@RequestParam(required = false) String sort,
			@RequestParam(required = false) String direction){

		//SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuilder sb = new StringBuilder();
		sb.append(" 	from TProductOrder ord  where 1=1	");
		
		if(!StringUtils.isEmpty(order.getOrderCode())){		/*基本条件查询*/
				sb.append(" 	and ord.orderCode ='"+ order.getOrderCode()+"'");
		}
		if(!StringUtils.isEmpty(order.getDistributorCode())){
			sb.append(" 	and ord.distributorCode ='"+ order.getDistributorCode()+"'");
		}
		if(!StringUtils.isEmpty(order.getProductCode())){
			sb.append(" 	and ord.productCode ='"+ order.getProductCode()+"'");
		}
		if(!StringUtils.isEmpty(startTime)){			/*时间范围查询*/
			sb.append("	and ord.createTime > '"+ startTime+"'");
		}
		if(!StringUtils.isEmpty(endTime)){
			sb.append("	and ord.createTime < '"+ endTime+"'");
		}
		
		if(sort!=null && direction!=null){		/*字段排序*/
			sb.append("  order by 	"+sort+"		"+direction);
		}
		
		String rowSql = sb.toString();
		String countSql = "	select count(*) 	"+ rowSql;
		
		Pagination<Object> page = IOrderService.findPageByHQL(rowSql, countSql, startIndex, pageSize);
		
		return page;
	}
}
